#ifndef __MINIUTIL_UTIL_SCOPE_H__
#define __MINIUTIL_UTIL_SCOPE_H__

#include <list>
#include <vector>

/*
	090630
	[0, 30] 	[35, 100] 	[100], 200]
	will merge to 
	[0, 30] [35, 200]
*/
namespace miniutil { namespace util {
	
template <class TN>
class Scope
{
public:
	typedef std::pair<TN, TN> SCOPE_PAIR;

	int addScope(SCOPE_PAIR scope)
	{
		typename std::list<SCOPE_PAIR>::iterator iter_ptt, iter_find;
		iter_find = allscopes.end();
		for(iter_ptt = allscopes.begin(); iter_ptt != allscopes.end();iter_ptt++)
		{
			if(std::less_equal<TN>()(iter_ptt->first, scope.first))
			{
				iter_find = iter_ptt;
			}
		}
		if(iter_find != allscopes.end())
		{
			typename std::list<SCOPE_PAIR>::iterator iter_ptt_tmp = iter_find;
			iter_ptt_tmp ++;
			allscopes.insert(iter_ptt_tmp, scope);
		}
		else
		{
			allscopes.insert(allscopes.begin(), scope);
			iter_find = allscopes.begin();
		}
		mergeScope(iter_find);
		return 0;
	}

	bool hasScope(SCOPE_PAIR scope)
	{
        typename std::list<SCOPE_PAIR>::reverse_iterator iter_lpff;
        for(iter_lpff = allscopes.rbegin(); iter_lpff != allscopes.rend();
			iter_lpff ++)
        {
			if(iter_lpff->first <= scope.first)
            {
			    if(iter_lpff->first + iter_lpff->second >= scope.first + scope.second)
			    {
				    return true;
                }
                else
                {
                    return false;
                }
			}
        }
        return false;


		//typename std::list<SCOPE_PAIR>::iterator iter_lpff;

		//for(iter_lpff = allscopes.begin(); iter_lpff != allscopes.end();
		//	iter_lpff ++)
		//{
		//	if(iter_lpff->first <= scope.first)
		//		break;
		//}
		//int ret = 0;
		//if(iter_lpff != allscopes.end())
		//{
		//	if(iter_lpff->first + iter_lpff->second >= scope.first + scope.second)
		//	{
		//		return true;
		//	}
		//}
		//return false;
	}

	//rmvScope();

	std::list<SCOPE_PAIR > getScopes()
	{
		return allscopes;
	}

    void clearAll()
    {
        allscopes.clear();
    }
private:

	int mergeScope(typename std::list<SCOPE_PAIR>::iterator refiter)
	{
		if(allscopes.size() >= 2 && allscopes.end() != refiter)
		{
			while(refiter != allscopes.end())
			{
				typename std::list<SCOPE_PAIR>::iterator iter_ptt_tmp = refiter;
				typename std::list<SCOPE_PAIR>::iterator iter_ptt_next = ++iter_ptt_tmp;
				if(iter_ptt_next == allscopes.end())
					break;

				if(std::less_equal<TN>()(refiter->first, iter_ptt_next->first)
					&& std::greater_equal<TN>()(
						refiter->first + refiter->second,iter_ptt_next->first))
				{
					TN a1 = refiter->first + refiter->second;
					TN a2 = iter_ptt_next->first + iter_ptt_next->second;
					refiter->second = a1>a2?
						refiter->second:a2-refiter->first;
					allscopes.erase(iter_ptt_next);
					continue;
				}
				break;
			}
		}
		return 0;
	}

	typename std::list<SCOPE_PAIR > allscopes; 

};

} }
#endif
